import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        headError: 'this.src="' + require('@/assets/img/defaultimg.png') + '"', //默认头像
        imgError: 'this.src="' + require('@/assets/img/no-head.png') + '"', //默认图片
        token: localStorage.getItem("TokenKey"),
        userId: localStorage.getItem("userId"),
        openId:"",
        maskAct: false,
        changeaudio: "",
        poeId:'',
        roomInfo:{},        //飞花令匹配房间的信息
        socket: null,
        aid:'001',
        uid: guid(),
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_OPENID: (state, openId) => {
            state.openId = openId;
        },
        SET_USERID: (state, userId) => {
            state.userId = userId;
        },
        Set_MaskAct:(state,type) => {                 
            state.maskAct = type;
        },
        set_ChangeAudio(state, type) {
            state.changeaudio = type;
        },
        SET_ROOMINFO(state, type) {
            state.roomInfo = type;
        },
        SET_SOCKET(state, data) {
            state.socket = data;
        },
        set_poeId(state, type) {
         state.poeId = type;
        },

    },
    getters:{
        token: state => state.token,
        getMaskAct: state => state.maskAct,
        headError: state => state.headError,
        getChangeAudio: state => state.changeaudio,
        getPoeId: state=>state.poeId
    },
    actions: {
        setToken({commit},data){
            commit('SET_TOKEN', data);
        },
        setOpenId({commit},data){
            commit('SET_OPENID', data);
        },
        setUserId({commit},data){
            commit('SET_USERID', data);
        },
        setMaskAct: ({commit}, view) => {
            commit('Set_MaskAct', view)
        },
        setPoeId({ commit }, data) {

            commit('set_poeId', data)

        },
        //登出
        FedLogOut({ commit }) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '');
                localStorage.clear()
                resolve();
            });
        },
        //切换音频
        setChangeAudio ({ commit }, type) {  
            commit("set_ChangeAudio", type)
        },
        setChangeAudio ({commit}, type) {
            commit("set_ChangeAudio", type)
        },
        // 飞花令
        setRoomInfo: ({commit}, data) => {
            commit('SET_ROOMINFO', data)
        },
        SocketConnect({ commit, state }, url) {
            return new Promise(resolve => {
                var socket = io.connect(url)
                commit('SET_SOCKET', socket);
                console.log("触发SocketConnect")
                socket.on('connect', function() {
                    console.log("连接")
                });
                // 服务端保持通讯
                socket.on('server_pong', function() {
                    console.log("server_pong")
                })
                socket.on('connect_error', (error) => {
                    socket.close();
                });
                resolve(socket);
            });
        },
        DisSocketConnect({ commit }) {
            return new Promise(resolve => {
                commit('SET_SOCKET', null);
                resolve();
            });
        },

    }
})
function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}
